package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    //查询今日运营数据
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        log.info("查询今日运营数据");
        Map map=new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);
        //查询今日新增用户数
        Integer newUsers = userMapper.countByMap(map);
        //查询有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(map);
        //查询营业额
        Double sumByMap = orderMapper.sumByMap(map);
        Double turnover = sumByMap == null ? 0 : sumByMap;

        Double unitPrice = 0.0;
        Double orderCompletionRate = 0.0;
        if(totalOrderCount!=0&&validOrderCount!=0){
            //计算平均单价
            unitPrice=turnover/validOrderCount;
            //计算订单完成率
            orderCompletionRate=validOrderCount.doubleValue()/totalOrderCount;
        }
        return BusinessDataVO.builder()
                .turnover(turnover)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .orderCompletionRate(orderCompletionRate)
                .validOrderCount(validOrderCount)
                .build();
    }

    @Override
    public OrderOverViewVO getOrderOverView(LocalDateTime begin) {
        Map map=new HashMap();
        map.put("begin",begin);
        //查询待接单数
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);
        //查询取消订单数
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //查询待派送订单数
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);
        //查询完成订单数
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //查询总订单数
        map.put("status",null);
        Integer allOrders = orderMapper.countByMap(map);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .build();
    }

    //查询菜品总览
    @Override
    public DishOverViewVO getDishOverView() {
        Map map=new HashMap();
        map.put("status", StatusConstant.DISABLE);
        //查询停售中的菜品总数
        Integer discontinued = dishMapper.countByMap(map);
        //查询起售中的菜品总数
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    //查询套餐总览

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map=new HashMap();
        map.put("status", StatusConstant.DISABLE);
        //查询停售中的菜品总数
        Integer discontinued = setmealMapper.countByMap(map);
        //查询起售中的菜品总数
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }
}
